﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using SubSonic.Query;
using SubSonic.Schema;
using DataModel;

namespace DataAccess.DbHelper {

	public class SelectHelper {
        private SubSonic.DataProviders.IDataProvider _provider;
        public SelectHelper()
        {
            _provider = new SUPNETDBDB().Provider;
        }

		#region 获取记录总数 GetRecordCount

		/// <summary>
		/// 获取记录总数
		/// </summary>
		/// <typeparam name="T">泛类</typeparam>
		/// <param name="wheres">条件</param>
		/// <param name="norepeat">是否去重复</param>
		/// <returns></returns>
        //public int GetRecordCount<T>(List<ConditionHelper.SqlqueryCondition> wheres = null, bool norepeat = false) where T : IActiveRecord, new() 
        public int GetRecordCount<T>(List<ConditionHelper.SqlqueryCondition> wheres, bool norepeat) where T : IActiveRecord, new() {
			int count = 0;

			try {
                SqlQuery select = new Select(_provider).From<T>();

				//设置是否去重复
				select.Distinct(norepeat);

				//判断是否有条件
				if (wheres != null) {
					//添加条件
					ConditionHelper.SqlqueryCondition.AddSqlqueryCondition(select, wheres);
				}

				count = select.GetRecordCount();
			}
			catch (Exception) { }

			return count;
		}
		#endregion


		#region 获取一个DataTable
		/// <summary>
		/// 获取一个查询（SubSonic.Query.Select）
		/// </summary>
		/// <typeparam name="T">泛类型</typeparam>
		/// <param name="norepeat">是否去重复</param>
		/// <param name="top">查询条数，0=全部记录</param>
		/// <param name="columns">要显示的列</param>
		/// <param name="pageIndex">当前页面，等于0时表示不分页，即查询全部记录</param>
		/// <param name="pageSize">页面大小，即当前页面显示多少条记录</param>
		/// <param name="wheres">条件</param>
		/// <param name="orderbys">排序</param>
		/// <returns>实体类</returns>
        //public DataTable GetDataTable<T>(bool norepeat = false, int top = 0, List<string> columns = null, int pageIndex = 1, int pageSize = 10, List<ConditionHelper.SqlqueryCondition> wheres = null, List<string> orderbys = null) where T : IActiveRecord, new() {
        public List<T> GetDataTable<T>(bool norepeat, int top, List<string> columns, int pageIndex, int pageSize , List<ConditionHelper.SqlqueryCondition> wheres, List<string> orderbys) where T : IActiveRecord, new() {
			Select select = null;
			//判断是否查询指定的字段
			//指定列
			if (columns != null) {
                select = new Select(_provider, columns.ToArray());
			}
			//全部字段
			else {
                select = new Select(_provider);
			}

			select.From<T>();

			//设置是否去重复
			select.Distinct(norepeat);

			//设置查询数量
			if (top > 0) {
				select.Top(top.ToString());
			}

			//判断是否有条件
			if (wheres != null) {
				//添加条件
				ConditionHelper.SqlqueryCondition.AddSqlqueryCondition(select, wheres);
			}

			//判断是否添加排序
			if (orderbys != null) {
				select.OrderBys = orderbys;
			}

			//设置分页
			if (pageIndex > 0 && pageSize > 0) {
				select.Paged(pageIndex, pageSize);
			}

			return select.ExecuteTypedList<T>();
		}

		#endregion

	}


}
